package com.pswv2.backstage.more.requirements.answer.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.more.requirements.answer.service.AnswerRequirementsService;
import com.pswv2.bub.bsc.dao.po.AnswerRequirements;
import com.pswv2.bub.bsc.dao.po.DataManagement;
import com.pswv2.bub.bsc.dao.po.ReleaseInformation;
import com.pswv2.bub.bsc.dao.po.User;
import com.pswv2.bub.bsc.dao.po.UserRequirements;
import com.pswv2.bub.bsc.dao.po.UserUserRequirements;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;

@Controller
@RequestMapping(value = "answerRequirementsAction", produces = { "text/json;charset=UTF-8" })
public class AnswerRequirementsAction {
	private static Class<?> poc = AnswerRequirements.class;
	private static Class<?> pocUR = UserRequirements.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Autowired
	private AnswerRequirementsService answerRequirementsService;
	/**
	 * 中等模式匹配结构
	 * userRequirementsId 需求Id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("queryMediumAnswer")
	public String queryMediumAnswer(int userRequirementsId,int userId) {
		UserRequirements entityUR=  extraSpringHibernateTemplate.findFirstOneByPropEq(pocUR, "userRequirementsId", userRequirementsId);
		Integer territory =entityUR.getTerritoryId();
		String term1=entityUR.getTerm1();
		String term2=entityUR.getTerm2();
		String kword=entityUR.getkWord();
		String method =entityUR.getMethod();
		List<ReleaseInformation> ls=(List<ReleaseInformation>) extraSpringHibernateTemplate.getHibernateTemplate().find("From ReleaseInformation where territoryId=? and   kWord like ? and userId!=?",territory,"%"+kword+"%",userId);
		List<Map<String ,Object>> ls1=new ArrayList<Map<String ,Object>>();
		for(int i=0;i<ls.size();i++){
				Map<String ,Object> map =new HashMap<String ,Object>();
				User entityU=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", ls.get(i).getUserId());
				map.put("name", entityU.getName());
				if(ls.get(i).getTerm1().contains(term1)&&ls.get(i).getTerm2().contains(term2)&&ls.get(i).getMethod().contains(method)){
					ls.get(i).setTemporaryOrder(0);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else if(ls.get(i).getTerm1().contains(term1)&&ls.get(i).getTerm2().contains(term2)){
					ls.get(i).setTemporaryOrder(1);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else if(ls.get(i).getTerm2().contains(term2)&&ls.get(i).getMethod().contains(method)){
					ls.get(i).setTemporaryOrder(2);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else if(ls.get(i).getTerm1().contains(term1)&&ls.get(i).getMethod().contains(method)){
					ls.get(i).setTemporaryOrder(3);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else if(ls.get(i).getTerm1().contains(term1)){
					ls.get(i).setTemporaryOrder(4);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else if(ls.get(i).getTerm2().contains(term2)){
					ls.get(i).setTemporaryOrder(5);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else if(ls.get(i).getMethod().contains(method)){
					ls.get(i).setTemporaryOrder(6);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}else{
					ls.get(i).setTemporaryOrder(7);
					map.put("releaseInformation", ls.get(i));
					ls1.add(map);
				}
			}
		return CommonResp.buildSuccessResp(ls1).toJsonString();
	}	
	/**
	 * 通过需求Id和用户Id查询到唯一的回答
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("queryByURIdUId")
	public String queryByURIdUId(int userId,int userRequirementsId) {
		List<AnswerRequirements> lsar=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userId=? and userRequirementsId=?", userId,userRequirementsId);
		if(lsar.size()>0){
			AnswerRequirements ar=lsar.get(0);
			return CommonResp.buildSuccessResp(ar).toJsonString();
		}else{
			return CommonResp.buildSuccessResp().toJsonString();
		}
	}
	/**
	 * app查看回答
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("seeAnswer")
	public String seeAnswer(int answerRequirementsId,int userRequirementsId) {
		AnswerRequirements entityAR=answerRequirementsService.queryById(answerRequirementsId);
		if(entityAR.getStatusSee()==AnswerRequirements.wait_see){
			entityAR.setStatusSee(AnswerRequirements.already_see);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entityAR);
		}
		List<AnswerRequirements> ls=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where statusSee>? and userRequirementsId=?", AnswerRequirements.wait_see,userRequirementsId);
		List<DataManagement> ls1=(List<DataManagement>) extraSpringHibernateTemplate.findAll(DataManagement.class);
		DataManagement dm=ls1.get(0);
		if(ls.size()<=dm.getRequirementReleaseAmount()){
			Map<String,Object> map=new HashMap<String,Object>();
			int userId1=entityAR.getUserId();
			User us=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", userId1);
			map.put("answerName", us.getName());
			map.put("ar", entityAR);
			return CommonResp.buildSuccessResp(map).toJsonString();
		}else{
			entityAR.setStatusSee(AnswerRequirements.wait_see);
			extraSpringHibernateTemplate.getHibernateTemplate().update(entityAR);
			return CommonResp.buildErrorResp(BackendErrorCode.overstep_see).toJsonString();
		}
	}
	/**
	 * app行用度的评价
	 * @return
	 */
	@ResponseBody
	@RequestMapping("changeDegreeOSPoinst")
	public String changeDegreeOSPoinst(double degreeOfSatisfaction,int answerRequirementsId) {
		AnswerRequirements entityAR=answerRequirementsService.queryById(answerRequirementsId);
		entityAR.setDegreeOfSatisfaction(degreeOfSatisfaction);
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(UserRequirements.class, "userRequirementsId", entityAR.getUserRequirementsId());
		entityAR.setStatusSee(AnswerRequirements.discuss);
		long answerTime=entityAR.getAnswerTime();
		long sd=answerTime-ur.getReleaseTime();
		long dd=sd/(1000 * 60 * 60 *24);
		double returnPionts=entityAR.getRemunerationPoints();
		entityAR.setActualReturnPoints(returnPionts*(degreeOfSatisfaction/10)/(dd+1)*8/10);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entityAR);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app行用度的评价
	 * @return
	 */
	@ResponseBody
	@RequestMapping("changeDegreeOS")
	public String changeDegreeOS(double degreeOfSatisfaction,int answerRequirementsId) {
		AnswerRequirements entityAR=answerRequirementsService.queryById(answerRequirementsId);
		entityAR.setDegreeOfSatisfaction(degreeOfSatisfaction);
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(UserRequirements.class, "userRequirementsId", entityAR.getUserRequirementsId());
		entityAR.setStatusSee(AnswerRequirements.discuss);
		long answerTime=entityAR.getAnswerTime();
		long sd=answerTime-ur.getReleaseTime();
		long dd=sd/(1000 * 60 * 60 *24);
		double returnPrice=entityAR.getRemunerationPrice();
		entityAR.setActualReturnPrice(returnPrice*(degreeOfSatisfaction/10)/(dd+1)*8/10);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entityAR);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app通过id查询
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryById")
	public String queryById(int answerRequirementsId) {
		AnswerRequirements entityAR=answerRequirementsService.queryById(answerRequirementsId);
		Map<String,Object> map=new HashMap<String,Object>();
		int userId=entityAR.getUserId();
		User us=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", userId);
		map.put("answerName", us.getName());
		map.put("ur", entityAR);
		return CommonResp.buildSuccessResp(map).toJsonString();
	}
	
	/**
	 * app改变状态
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("changeStatus")
	public String changeStates(int status,int answerRequirementsId) {
		AnswerRequirements entityAR=answerRequirementsService.queryById(answerRequirementsId);
		//entityAR.setStatus(status);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entityAR);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app查看有多少回答
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryMyAnswer")
	public String queryMyAnswer(int userRequirementsId) {
		List<Map<String,Object>> arrayList=new ArrayList<>();
		List<AnswerRequirements> ar=extraSpringHibernateTemplate.findAllByPropEq(poc, "userRequirementsId", userRequirementsId);
		for(AnswerRequirements EntityAP:ar){
			Map<String,Object> map=new HashMap<String,Object>();
			int userId= EntityAP.getUserId();
			User us=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", userId);
			map.put("answerName", us.getName());
			map.put("ur", EntityAP);
			arrayList.add(map);
		}
		return CommonResp.buildSuccessResp(arrayList).toJsonString();
	}
	/**
	 * app查看是否有用户回答
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryUserAnswer")
	public String queryUserAnswer(int userId) {
		List<AnswerRequirements> arrayList=new ArrayList<>();
		List<AnswerRequirements> ar=extraSpringHibernateTemplate.findAllByPropEq(poc, "userId", userId);
		for(AnswerRequirements EntityAR:ar){
			if(EntityAR.getStatusSee()==0){
				arrayList.add(EntityAR);
			}
		}
		return CommonResp.buildSuccessResp(arrayList).toJsonString();
	}
	/**
	 * app查看是否有用户回答
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryUserAnswer1")
	public String queryUserAnswer1(int userId) {
		List<UserRequirements> lsUR=new ArrayList<>();
		List<AnswerRequirements> ar=extraSpringHibernateTemplate.findAllByPropEq(poc, "userId", userId);
		for(AnswerRequirements EntityAR:ar){
			if(EntityAR.getStatusSee()==0){
				int uid=EntityAR.getUserRequirementsId();
				UserRequirements ur= extraSpringHibernateTemplate.findFirstOneByPropEq (UserRequirements.class, "userRequirementsId", uid);
				lsUR.add(ur);
			}
		}
		return CommonResp.buildSuccessResp(lsUR).toJsonString();
	}
	/**
	 * app添加
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("appAdd")
	public String appAdd(int userId,int userRequirementsId,
			String answer,String kWord) {
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(UserRequirements.class, "userRequirementsId", userRequirementsId);
		List<AnswerRequirements> ls=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userId=? and userRequirementsId=? ", userId,userRequirementsId);
		if(ls.size()==0){
			AnswerRequirements answerRequirements=new AnswerRequirements();
			Long s=new Date().getTime();
			double price=ur.getPrice();
			double points=ur.getPoints();
			answerRequirements.setAnswerTime(s);
			answerRequirements.setUserId(userId);
			answerRequirements.setAnswer(answer);
			answerRequirements.setRemunerationPrice(price);
			answerRequirements.setRemunerationPoints(points);
			answerRequirements.setUserRequirementsId(userRequirementsId);
			answerRequirements.setkWord(kWord);
			answerRequirements.setStatusSee(AnswerRequirements.wait_see);
			extraSpringHibernateTemplate.getHibernateTemplate().save(answerRequirements);
			ur.setStatus(UserRequirements.status_answer);
			List<UserUserRequirements> lsuur=(List<UserUserRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", userId,userRequirementsId);
			if(lsuur.size()!=0){
				UserUserRequirements uur=lsuur.get(0);
				uur.setStatus(1);
				extraSpringHibernateTemplate.getHibernateTemplate().update(uur);
			}
			extraSpringHibernateTemplate.getHibernateTemplate().update(ur);
		}else{
			AnswerRequirements answerRequirements=ls.get(0);
			answerRequirements.setAnswer(answer);
			List<UserUserRequirements> lsuur=(List<UserUserRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", userId,userRequirementsId);
			if(lsuur.size()!=0){
				UserUserRequirements uur=lsuur.get(0);
				uur.setStatus(1);
				extraSpringHibernateTemplate.getHibernateTemplate().update(uur);
			}
			extraSpringHibernateTemplate.getHibernateTemplate().update(answerRequirements);
		}
		
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app添加
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("appAddPoints")
	public String appAddPoints(int userId,int userRequirementsId,
			String answer,String kWord) {
		UserRequirements ur=extraSpringHibernateTemplate.findFirstOneByPropEq(UserRequirements.class, "userRequirementsId", userRequirementsId);
		List<AnswerRequirements> ls=(List<AnswerRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From AnswerRequirements where userId=? and userRequirementsId=? ", userId,userRequirementsId);
		if(ls.size()==0){
			AnswerRequirements answerRequirements=new AnswerRequirements();
			Long s=new Date().getTime();
			double points=ur.getPoints();
			answerRequirements.setAnswerTime(s);
			answerRequirements.setUserId(userId);
			answerRequirements.setAnswer(answer);
			answerRequirements.setRemunerationPoints(points);
			answerRequirements.setUserRequirementsId(userRequirementsId);
			answerRequirements.setkWord(kWord);
			answerRequirements.setStatusSee(AnswerRequirements.wait_see);
			extraSpringHibernateTemplate.getHibernateTemplate().save(answerRequirements);
			ur.setStatus(UserRequirements.status_answer);
			List<UserUserRequirements> lsuur=(List<UserUserRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", userId,userRequirementsId);
			if(lsuur.size()!=0){
				UserUserRequirements uur=lsuur.get(0);
				uur.setStatus(1);
				extraSpringHibernateTemplate.getHibernateTemplate().update(uur);
			}
			extraSpringHibernateTemplate.getHibernateTemplate().update(ur);
		}else{
			AnswerRequirements answerRequirements=ls.get(0);
			answerRequirements.setAnswer(answer);
			List<UserUserRequirements> lsuur=(List<UserUserRequirements>) extraSpringHibernateTemplate.getHibernateTemplate().find("From UserUserRequirements where userId=? and userRequirementsId=?", userId,userRequirementsId);
			if(lsuur.size()!=0){
				UserUserRequirements uur=lsuur.get(0);
				uur.setStatus(1);
				extraSpringHibernateTemplate.getHibernateTemplate().update(uur);
			}
			extraSpringHibernateTemplate.getHibernateTemplate().update(answerRequirements);
		}
		
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * app修改
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appEdit")
	public String appEdit(int setAnswerRequirementsId,String answer) {
		AnswerRequirements entity=answerRequirementsService.queryById(setAnswerRequirementsId);
		entity.setAnswer(answer);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
		return CommonResp.buildSuccessResp().toJsonString();
	}

}
